//
//  BillConfig.m
//  YOYOLive
//
//  Created by Mac on 16/11/28.
//  Copyright © 2016年 王根强. All rights reserved.
//

#import "BillConfig.h"

@interface BillConfig () {
    NSUserDefaults *userDefaults;
}
@end

@implementation BillConfig
@synthesize _billsArray;

+ (BillConfig *)shareInstance {
    static BillConfig *instance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        instance = [[BillConfig alloc] init];
    });
    return instance;
}

- (void)startcheck {
    NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:15.0 target:self selector:@selector(checkBill) userInfo:nil repeats:YES];
}

- (void)userDefaultsInit {
    if (!userDefaults) {
        userDefaults = [NSUserDefaults standardUserDefaults];
    }
}

#pragma mark setter-method
- (void)set_billsArray:(NSArray *)billsArray {
    if (_billsArray != billsArray) {
        [self userDefaultsInit];
        [userDefaults setObject:billsArray forKey:@"billArray"];
        [userDefaults synchronize];
    }
}

#pragma mark getterMethod
- (NSArray *)_billsArray {
    [self userDefaultsInit];
    NSArray *billsArray = [userDefaults objectForKey:@"billArray"];
    return billsArray;
}

- (void)checkBill {
    if (self._billsArray.count > 0) {
        for (int i = 0; i<self._billsArray.count; i++) {
            NSString * str =self._billsArray[i];
            NSMutableDictionary * postDic = [[NSMutableDictionary alloc] init];
            [postDic setObject:[UserConfig shareInstance]._appUserID forKey:@"userId"];
            [postDic setObject:str forKey:@"receiptData"];
            NSString *url = [NSString stringWithFormat:@"%@%@",PRODUCTION_BASE_URL, API_REQUEST_ADDRMBZUANIAPORDER];
            [YONetwork postWithURL:url withParams:postDic withSuccessBlock:^(NSDictionary *dic) {
                NSMutableArray * tempArray = [NSMutableArray arrayWithArray:self._billsArray];
                [tempArray removeObject:str];
                self._billsArray = tempArray;
            } withFailureBlock:^(NSError *error) {
                
            }];
        }
    }
}

@end
